﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using System.Windows.Forms;
using System.Drawing;
using Noris.Schedule.Support.Core;

namespace Noris.Schedule.UI.Components
{
    /// <summary>
    /// Panel pro interaktivní zadání fontu
    /// </summary>
    public class FontSelectPanel : DPanel
    {
        #region Konstrukce
        /// <summary>
        /// Konstruktor
        /// </summary>
        public FontSelectPanel()
        {
            this.Initialize();
        }
        /// <summary>
        /// Inicializace objektu
        /// </summary>
        protected void Initialize()
        {
            this.SuspendLayout();

            this.Name = "_FontPanel";

            this.CreateInnerObjects();

            string serialized = this.Serialized;         // Font a barva se serializuje,
            this.Serialized = serialized;                //  a deserializuje podle pravidel (tím se korektně promítně do controlů)

            this.ResumeLayout(false);
            this.PerformLayout();
        }
        /// <summary>
        /// Vytvoří vnitřní objekty
        /// </summary>
        protected void CreateInnerObjects()
        {
            this.Size = new System.Drawing.Size(403, 29);

            this.FontLabel = new System.Windows.Forms.Label();
            this.FontText = new System.Windows.Forms.Label();
            this.FontButton = new System.Windows.Forms.Button();
            this.Controls.Add(this.FontText);
            this.Controls.Add(this.FontLabel);
            this.Controls.Add(this.FontButton);

            // FontLabel
            this.FontLabel.AutoSize = true;
            this.FontLabel.Location = new System.Drawing.Point(3, 8);
            this.FontLabel.Name = "_FontLabel";
            this.FontLabel.Size = new System.Drawing.Size(40, 13);
            this.FontLabel.TabIndex = 1;
            this.FontLabel.Text = "Písmo:";

            // FontText
            this.FontText.Anchor = AnchorStyles.Top | AnchorStyles.Bottom | AnchorStyles.Left | AnchorStyles.Right;
            this.FontText.BackColor = Color.LightYellow;
            this.FontText.BorderStyle = BorderStyle.FixedSingle;
            this.FontText.Location = new Point(49, 1);
            this.FontText.Name = "_FontText";
            this.FontText.Padding = new System.Windows.Forms.Padding(5, 0, 5, 0);
            this.FontText.Size = new Size(324, 27);
            this.FontText.TabIndex = 2;
            this.FontText.Text = "";
            this.FontText.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;

            // FontButton
            this.FontButton.Anchor = AnchorStyles.Top | AnchorStyles.Right;
            this.FontButton.Location = new System.Drawing.Point(373, 1);
            this.FontButton.Name = "_FontButton";
            this.FontButton.Size = new System.Drawing.Size(25, 27);
            this.FontButton.TabIndex = 3;
            this.FontButton.Text = "...";
            this.FontButton.UseVisualStyleBackColor = true;
            this.FontButton.Click += this._FontButton_Click;
        }
        protected Label FontLabel;
        protected Label FontText;
        protected Button FontButton;
        #endregion
        #region Public property a eventy
        /// <summary>
        /// Pozice X, na které začíná text. Dovoluje tak zarovnat textové prvky.
        /// </summary>
        [Category("FontSelectPanel")]
        [Description("Pozice X, na které začíná text. Dovoluje tak zarovnat textové prvky.")]
        public int TextPosition
        {
            get { return this.FontText.Left; }
            set { this.FontText.Left = value; this.FontText.Width = (this.FontButton.Left - value - 0); }
        }
        /// <summary>
        /// Aktuálně vybraný font
        /// </summary>
        [Category("FontSelectPanel")]
        [Description("Aktuálně vybraný font")]
        public Font SelectedFont
        {
            get { return this.FontText.Font; }
            set { this.FontShow(value, this.FontText.ForeColor); }
        }
        /// <summary>
        /// Aktuálně vybraná barva písma (popředí)
        /// </summary>
        [Category("FontSelectPanel")]
        [Description("Aktuálně vybraná barva písma (popředí)")]
        public Color SelectedForeColor
        {
            get { return this.FontText.ForeColor; }
            set { this.FontText.ForeColor = value; }
        }
        /// <summary>
        /// Aktuálně vybraná barva podkladu (pozadí)
        /// </summary>
        [Category("FontSelectPanel")]
        [Description("Aktuálně vybraná barva podkladu (pozadí)")]
        public Color SelectedBackColor
        {
            get { return this.FontText.BackColor; }
            set { this.FontText.BackColor = value; }
        }
        /// <summary>
        /// Kompletní serializovaná data
        /// </summary>
        [Category("FontSelectPanel")]
        [Description("")]
        public virtual string Serialized
        {
            get { return this.GetSerialized(); }
            set { this.SetSerialized(value); this._RefreshTooltips(); }
        }
        /// <summary>
        /// Událost po interaktivní změně fontu nebo barvy
        /// </summary>
        public event EventHandler SelectedFontChanged;
        protected virtual void OnSelectedFontChanged()
        {
            if (this.SelectedFontChanged != null)
                this.SelectedFontChanged(this, EventArgs.Empty);
        }
        #endregion
        #region Protected property a eventy
        private void _RefreshTooltips()
        {
            this.FontText.SetToolTipText("Ukázka vybraného písma");
            this.FontButton.SetToolTipText("Umožní zvolit písmo");
        }
        private void _FontButton_Click(object sender, EventArgs e)
        {
            using (System.Windows.Forms.FontDialog fd = new FontDialog())
            {
                fd.AllowScriptChange = false;
                fd.AllowSimulations = false;
                fd.AllowVectorFonts = true;
                fd.AllowVerticalFonts = false;
                fd.Color = FontText.ForeColor;
                fd.FixedPitchOnly = false;
                fd.Font = FontText.Font;
                fd.FontMustExist = true;
                fd.MaxSize = 18;
                fd.MinSize = 6;
                fd.ShowApply = true;
                fd.ShowColor = true;
                fd.ShowEffects = true;
                fd.ShowHelp = false;
                fd.Apply += this.FontApply;
                DialogResult result = fd.ShowDialog();
                if (result == System.Windows.Forms.DialogResult.OK)
                {
                    this.FontShow(fd.Font, fd.Color);
                    this.OnSelectedFontChanged();
                }
            }
        }
        protected void FontApply(object sender, EventArgs e)
        {
            System.Windows.Forms.FontDialog fd = sender as FontDialog;
            if (fd != null)
            {
                this.FontShow(fd.Font, fd.Color);
                this.OnSelectedFontChanged();
            }
        }
        protected void FontShow(Font font, Color foreColor)
        {
            this.FontShow(font, foreColor, Color.Empty);
        }
        protected void FontShow(Font font, Color foreColor, Color backColor)
        {
            if (font != null)
            {
                this.FontText.Font = font;
                this.FontText.Text = font.Name + "; " + font.SizeInPoints.ToString() + "; " + Convertor.FontStyleToString(font.Style);
            }
            if (!foreColor.IsEmpty)
                this.FontText.ForeColor = foreColor;
            if (!backColor.IsEmpty)
                this.FontText.BackColor = backColor;
        }
        protected virtual string GetSerialized()
        {
            return Noris.Schedule.Support.Expressions.ConfiguredSetting.GetSerial(this.SelectedFont, this.SelectedForeColor, this.SelectedBackColor);
        }
        protected virtual void SetSerialized(string value)
        {
            Noris.Schedule.Support.Expressions.ConfiguredSetting configuredFont = new Support.Expressions.ConfiguredSetting(value);
            if (configuredFont.Font != null)
                this.SelectedFont = configuredFont.Font;
            if (!configuredFont.ForeColor.IsEmpty)
                this.SelectedForeColor = configuredFont.ForeColor;
            if (!configuredFont.BackColor.IsEmpty)
                this.SelectedBackColor = configuredFont.BackColor;
        }
        #endregion
    }
}
